package net.dodoman.spider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class ParserUtil {
	public static final String DEFAULT_CHARSET = "GBK";

	public static final String GB2312_CHARSET = "gb2312";

	public static final String UTF8_CHARSET = "utf-8";

	public static final String HTML_SUFFIX = ".html";

	public static final String HEAD_TAG_NAME = "head";

	public static final String UL_TAG_NAME = "ul";

	public static final String LI_TAG_NAME = "li";

	public static final String A_TAG_NAME = "a";

	public static final String P_TAG_NAME = "p";

	public static final String H1_TAG_NAME = "h1";

	public static final String BODY_TAG_NAME = "body";

	public static final String TABLE_TAG_NAME = "table";

	public static final String TR_TAG_NAME = "tr";

	public static final String TD_TAG_NAME = "td";

	public static final String DIV_TAG_NAME = "div";

	public static final String IFRAME_TAG_NAME = "iframe";

	public static final String FONT_TAG_NAME = "font";

	public static final char SPLIT_STRING = ';';

	public static final int INDEX_OF_START = 9;

	public static final String MY_SEPARATOR = "|";

	public static final String SPECIAL_TEXT = "title";// 列表链接关键字1

	public static final String SPECIAL_TEXT1 = "Amenu";// 列表链接关键字2

	public static final String DETAIL_SPECIAL_TEXT = "class=NewsContent";

	public static final String A_REGEX_STRING = "\\d{4}\\-\\d{1,2}\\-\\d{1,2}";

	public static final String BLANK_REGEX_STRING = "\\[\\d{4}\\-\\d{1,2}\\-\\d{1,2}\\]";

	//日期类型exp:2007年10月1日
	public static final String Date_REGEX_STRING = "\\[\\d{4}\\u5E74\\d{1,2}\\u6708\\d{1,2}\\u65E5\\]";

	// 根据字串获取标题
	public static String getTitle(String string) {
		String title;
		String subStringBySplit[] = StringUtils.split(string,
				ParserUtil.SPLIT_STRING);
		title = subStringBySplit[1].substring(ParserUtil.INDEX_OF_START,
				subStringBySplit[1].length());
		title = StringUtils.trim(title);
		return title;
	}

	// 根据字串获取链接
	public static String getLink(String string) {
		String link;
		String subStringBySplit[] = StringUtils.split(string,
				ParserUtil.SPLIT_STRING);
		link = subStringBySplit[0].substring(ParserUtil.INDEX_OF_START,
				subStringBySplit[0].length());
		link = StringUtils.trim(link);
		return link;
	}

	public static List removeInvalidLink(List list) {
		Pattern p = Pattern.compile(A_REGEX_STRING);
		if (list.size() >= 1) {
			for (int i = 0; i < list.size(); i++) {
				Matcher m = p.matcher(list.get(i).toString());
				if (!m.find())
					list.remove(i);
				else
					System.out.println(list.get(i).toString());
			}
		}
		return list;
	}

	// 在正则表达式里配置垃圾字符并除去
	public static String replaceInvalidText(String string) {
		Pattern p = Pattern.compile(Date_REGEX_STRING);
		Matcher m = p.matcher(string);
		if (m.find(0)) {
			return m.replaceAll("");
		} else
			return string;
	}

	public static boolean isLinkNeed(String input) {
		Pattern pattern = Pattern.compile(ParserUtil.A_REGEX_STRING);
		Matcher m = pattern.matcher(input);
		if (m.find(0))
			return true;
		else
			return false;
	}

	// 返回整个Html文档
	public static HtmlDocument getAllHtml(String url) throws ParserException {
		StringBuffer sb = new StringBuffer();
		Parser parser = new Parser(url);
		HtmlDocument htmlDoc = new HtmlDocument();
		NodeList allList = parser.parse(null);
		for (int i = 0; i < allList.size(); i++) {
			sb.append(allList.elementAt(i).toHtml() + "\n");
		}
		htmlDoc.setHtmlResource(sb.toString());
		return htmlDoc;
	}

	// 保存为Html文件
	public static void generateHtmlFile(String string, String fileName)
			throws ParserException, IOException {
		File file = new File(fileName);
		FileOutputStream fos = new FileOutputStream(file);
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		osw.write(string);
		osw.flush();
		osw.close();
	}

	// 生成列表Html文件
	public static void genarateListHtml(List list) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			sb.append("<a href=\"" + ((HtmlDocument) list.get(i)).getLink()
					+ "\">" + ((HtmlDocument) list.get(i)).getTitle()
					+ "</a><br>");
			try {
				ParserUtil.generateHtmlFile(sb.toString(), "list1.html");
			} catch (ParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
